package ua.soso.service.impl;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Required;
import ua.soso.dao.FileCategoryDao;
import ua.soso.dao.FileDao;
import ua.soso.dao.UserDao;
import ua.soso.dao.model.File;
import ua.soso.dao.model.FileCategory;
import ua.soso.dao.model.FilePayload;
import ua.soso.dao.model.User;
import ua.soso.service.FileService;
import ua.soso.service.bean.FileBean;
import ua.soso.service.bean.FileCategoryBean;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Babka Aleksey
 * Date: 03.05.12
 */
public class FileServiceImpl implements FileService {
    private FileDao fileDao;
    private UserDao userDao;
    private FileCategoryDao fileCategoryDao;

    @Required
    public void setFileDao(FileDao fileDao) {
        this.fileDao = fileDao;
    }

    @Required
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Required
    public void setFileCategoryDao(FileCategoryDao fileCategoryDao) {
        this.fileCategoryDao = fileCategoryDao;
    }

    @Override
    public List<FileBean> getAllFiles(long userId) {
        List<File> fileList = fileDao.findAll(userId);
        List<FileBean> fileBeanList = new ArrayList<FileBean>();

        for (File file : fileList) {
            FileBean bean = new FileBean();

            bean.setId(file.getId());
            bean.setDescription(file.getDescription());
            bean.setCategory(file.getCategory().getName());
            bean.setName(file.getFileName());
            bean.setPublic(file.isPublished());
            bean.setUser(file.getUser().getLogin());

            fileBeanList.add(bean);
        }

        return fileBeanList;
    }

    @Override
    public List<FileBean> getUserFiles(long userId) {
        List<File> fileList = fileDao.findByUserId(userId);

        List<FileBean> fileBeanList = new ArrayList<FileBean>();

        for (File file : fileList) {
            FileBean bean = new FileBean(file);

            fileBeanList.add(bean);
        }

        return fileBeanList;
    }

    @Override
    public void saveFile(String description, long categoryId, long userId, String fileName, boolean isPublic, InputStream stream) throws IOException {
        File file = new File();

        file.setFileName(fileName);
        file.setDescription(description);
        file.setPublished(isPublic);

        User user = userDao.findById(userId);
        file.setUser(user);

        FileCategory category = fileCategoryDao.findById(categoryId);
        file.setCategory(category);

        FilePayload payload = new FilePayload();
        payload.setFile(file);
        payload.setPayload(IOUtils.toByteArray(stream));

        file.setPayload(payload);

        fileDao.save(file);
    }

    @Override
    public List<FileCategoryBean> getCategories() {
        List<FileCategory> categories = fileDao.findCategories();

        List<FileCategoryBean> categoryBeanList = new ArrayList<FileCategoryBean>();

        for (FileCategory category : categories) {
            FileCategoryBean bean = new FileCategoryBean();

            bean.setId(category.getId());
            bean.setName(category.getName());

            categoryBeanList.add(bean);
        }

        return categoryBeanList;
    }

    @Override
    public void getFile(String fileId, OutputStream outputStream) throws IOException {
        File file = fileDao.findById(Long.parseLong(fileId));

        FilePayload payload = file.getPayload();

        byte[] bytes = payload.getPayload();

        outputStream.write(bytes);
    }
}
